package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduAuthSchool.EduAuthSchoolKeyVO;
import com.jwsoft.manager.common.vo.eduAuthSchool.EduAuthSchoolQueryVO;
import com.jwsoft.manager.common.vo.eduAuthSchool.EduAuthSchoolVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.core.dao.model.EduAuthSchool;
import com.jwsoft.manager.core.dao.service.EduAuthSchoolService;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.integration.EduAuthSchoolIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
/**
 * 城区审核学校名单业务实现类
 *
 * @author hhj
 * @since 2022-12-28
 */
@ApiService(funcCode = "eduAuthSchool", title = "城区审核学校名单")
public class EduAuthSchoolIntegrationImpl implements EduAuthSchoolIntegration {

    @Autowired
    public EduAuthSchoolService eduAuthSchoolService;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private EduSchoolService eduSchoolService;

    @Override
    @OpApi(funcCode = "eduAuthSchool0001", title = "城区审核学校名单分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduAuthSchoolVO> getList(EduAuthSchoolQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        if (ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        QueryWrapper<EduAuthSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduAuthSchool::getYear,vo.getYear())
                .eq(!ObjectUtils.isEmpty(vo.getAreaCode()),EduAuthSchool::getAreaCode,vo.getAreaCode())
                .eq(!ObjectUtils.isEmpty(vo.getSchoolId()),EduAuthSchool::getSchoolId,vo.getSchoolId())
                .orderByDesc(EduAuthSchool::getSchoolName);
        List<EduAuthSchool> list = eduAuthSchoolService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduAuthSchool> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduAuthSchoolVO> resultList = Convert.toList(EduAuthSchoolVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduAuthSchool0002", title = "城区审核学校名单根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduAuthSchoolVO getById(EduAuthSchoolKeyVO vo) {
        EduAuthSchool entity = eduAuthSchoolService.getById(vo.getSchoolAuthId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduAuthSchoolVO result = Convert.convert(EduAuthSchoolVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduAuthSchool0003", title = "城区审核学校名单保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduAuthSchoolVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduAuthSchool0004", title = "城区审核学校名单新增", funcType = FuncTypeEnum.insert)
    @Transactional
    public EduAuthSchoolVO add(EduAuthSchoolVO vo) {
        //保存前校验
        if (ObjectUtils.isEmpty(vo.getAreaCode())){
            throw new AppException("行政区划不能为空");
        }
        if (ObjectUtils.isEmpty(vo.getSchoolId())){
            throw new AppException("学校不能为空");
        }
        if (ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        QueryWrapper<EduAuthSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduAuthSchool::getSchoolId,vo.getSchoolId())
                .eq(EduAuthSchool::getYear,vo.getYear());
        if (eduAuthSchoolService.count(queryWrapper) > 0){
            throw new AppException("该学校信息已存在，不能进行添加！");
        }
        EduSchoolVO detail = eduSchoolService.getDetail(vo.getSchoolId());
        if (!ObjectUtils.isEmpty(detail) && detail.getSchoolName() != null){
            vo.setSchoolName(detail.getSchoolName());
        }
        //保存数据
        EduAuthSchool entity = Convert.convert(EduAuthSchool.class, vo);
        entity.setCreateTime(new Date());
        entity.setCreateName(SessionUtil.getOrgId());
        entity.setUpdateName(SessionUtil.getOrgId());
        entity.setUpdateTime(new Date());
        eduAuthSchoolService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setSchoolAuthId(entity.getSchoolAuthId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAuthSchool0005", title = "城区审核学校名单修改", funcType = FuncTypeEnum.update)
    @Transactional
    public EduAuthSchoolVO edit(EduAuthSchoolVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolAuthId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduAuthSchool oldEntity = eduAuthSchoolService.getById(vo.getSchoolAuthId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduAuthSchool entity = Convert.convert(EduAuthSchool.class, vo);
        eduAuthSchoolService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduAuthSchool0006", title = "城区审核学校名单根据主键删除", funcType = FuncTypeEnum.delete)
    @Transactional
    public void del(EduAuthSchoolKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolAuthId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduAuthSchool entity = eduAuthSchoolService.getById(vo.getSchoolAuthId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduAuthSchoolService.removeById(vo.getSchoolAuthId());
    }
}
